{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b9238b75-f1ea-4e1f-bbea-757346913a15",
   "metadata": {},
   "source": [
    "# Exponential State Preparation\n",
    "\n",
    "The `prepare_exponential_state` function\n",
    "creates a state with exponentially decreasing amplitudes. Namely,\n",
    "the probability for a state representing an integer $n$ is\n",
    "\n",
    "$$\n",
    "P\\left(n\\right) = \\frac{1}{Z} e^{-\\lambda n}\n",
    "$$\n",
    "\n",
    "where $\\lambda$ is the rate, and $Z$ is a normalization factor.\n",
    "If $q$ in the number of qubits, then\n",
    "\n",
    "$$\n",
    "Z = \\sum_{n=0} ^{n = 2^q - 1} e^{-\\lambda n} = \\frac{1 - e^{-\\lambda 2^q}}{1 - e^{-\\lambda}}\n",
    "$$\n",
    "\n",
    "Function: `prepare_exponential_state`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `rate: CReal`\n",
    "- `q: QArray[QBit]`\n",
    "\n",
    "Notice that the function acts inplace on the qubits."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb5136ac-470c-445b-9de6-e9ac856249b9",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "Prepare a state with probabilities:\n",
    "$$\n",
    "P\\left(n\\right) = \\frac{1}{Z} e^{-0.1 n}\n",
    "$$\n",
    "where  $n$ is in the range $[0, 31]$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5752cf71-1d39-4b37-88de-0acf42ade6be",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:13.786126Z",
     "iopub.status.busy": "2024-05-07T13:26:13.784436Z",
     "iopub.status.idle": "2024-05-07T13:26:16.810646Z",
     "shell.execute_reply": "2024-05-07T13:26:16.810011Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    prepare_exponential_state,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QArray[QBit]]):\n",
    "    allocate(5, x)\n",
    "    prepare_exponential_state(0.1, x)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cf36a83d-8054-47db-aa31-ab4fec59c5af",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:16.813568Z",
     "iopub.status.busy": "2024-05-07T13:26:16.812990Z",
     "iopub.status.idle": "2024-05-07T13:26:18.098934Z",
     "shell.execute_reply": "2024-05-07T13:26:18.098166Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from classiq import synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"prepare_exponential_state\")\n",
    "qprog = synthesize(qmod)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
